<!--
 * @Author: QUAN
 * @Date: 2021-10-29 15:35:32
 * @LastEditTime: 2021-11-04 15:26:12
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \mutong-ketang-pc-mis\src\views\Activity\H5List.vue
-->
<template>
  <div class="h5-list">
    <search-list
      ref="h5list"
      :api-url="$url.SaleH5List"
      :option="searchOpt"
      :itemPerRow="6"
      :beforeQuery="beforeQuery"
      @selection-change="selectionChange"
    >
      <el-table-column type="selection"></el-table-column>
      <el-table-column prop="hid" label="H5页面ID"></el-table-column>
      <el-table-column prop="title" label="活动标题"></el-table-column>
      <el-table-column prop="nickname" label="H5类型">
        <template slot-scope="scope">
          <span>
            {{ $selectFormat['activityPagTypeSl'](scope.row.kind).label }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="H5页地址"></el-table-column>
      <el-table-column prop="online" label="状态">
        <template slot-scope="scope">
          {{ scope.row.online ? '上线' : '下线' }}
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="创建日期"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="detail(scope.row)">编辑</el-button>
          <el-button type="text" @click="deleteH5Item(scope.row)">删除</el-button>
          <el-button type="text" v-if="!scope.row.online" @click="onLine(scope.row)">
            上线
          </el-button>
          <el-button type="text" v-else @click="offLine(scope.row)">下线</el-button>
        </template>
      </el-table-column>
      <div slot="btn-after" class="btn-after">
        <el-button type="success" class="el-icon-plus" @click="add">
          新建活动H5页
        </el-button>
        <el-button type="primary" :disabled="selectedData.length === 0" @click="onLine('')">
          上线
        </el-button>
        <el-button type="primary" :disabled="selectedData.length === 0" @click="offLine('')">
          下线
        </el-button>
        <el-button type="danger" :disabled="selectedData.length === 0" @click="deleteH5Item">
          删除
        </el-button>
      </div>
    </search-list>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        // 多选
        selectedData: [],
        // 筛选
        searchOpt: [
          {
            type: this.SEARCH_COMP_ENUM.SEARCH_INPUT,
            name: 'hid',
            label: 'H5页面ID',
          },
          {
            type: 'activityPagTypeSl',
            name: 'kind',
            label: '全部',
          },
          {
            type: this.SEARCH_COMP_ENUM.SEARCH_DATEPICKER,
            name: 'updatedAt',
            label: '创建时间',
            conf: {
              type: 'datetimerange',
              format: 'yyyy-MM-dd HH:mm:ss',
              'value-format': 'yyyy-MM-dd HH:mm:ss',
              'start-placeholder': '创建开始时间',
              'end-placeholder': '创建结束时间',
            },
          },
        ],
      };
    },
    created() {},
    mounted() {},
    methods: {
      // 删除
      deleteH5Item(item) {
        this.$confirm('确定要删除这个H5页吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            this.$service
              .SaleH5Delete({
                ids: item.id
                  ? item.id
                  : this.selectedData
                      .map((item) => {
                        return item.id;
                      })
                      .join(','),
              })
              .then((res) => {
                this.$message.success('删除成功！');
                this.$refs.h5list.getList();
              });
          })
          .catch(() => {});
      },

      // 上线
      onLine(item) {
        this.$service
          .SaleH5Online({
            ids: item.id
              ? item.id
              : this.selectedData
                  .map((item) => {
                    return item.id;
                  })
                  .join(','),
          })
          .then((res) => {
            this.$message.success('操作成功！');
            this.$refs.h5list.getList();
          });
      },

      // 下线
      offLine(item) {
        this.$service
          .SaleH5Offline({
            ids: item.id
              ? item.id
              : this.selectedData
                  .map((item) => {
                    return item.id;
                  })
                  .join(','),
          })
          .then((res) => {
            this.$message.success('操作成功！');
            this.$refs.h5list.getList();
          });
      },

      // 编辑
      detail(item) {
        const { id } = item;
        this.$tabs.open({ name: 'ActivityH5Detail', params: { id } });
      },

      // 新增
      add() {
        this.$tabs.open({ name: 'ActivityH5Detail' });
      },

      // 多选
      selectionChange(val) {
        this.selectedData = [...val];
      },

      // 筛选之前
      beforeQuery({ searchInfo }) {
        if (searchInfo.updatedAt === null) {
          searchInfo.startDate = '';
          searchInfo.endDate = '';
        } else {
          searchInfo.startDate = searchInfo.updatedAt[0];
          searchInfo.endDate = searchInfo.updatedAt[1];
        }
      },
    },
  };
</script>
<style lang="less" scoped>
  .h5-list {
  }
</style>
